layui.use(['jquery','layer','table','form'],function() {
    var jquery = layui.jquery;
    var layer = layui.layer;
    var table = layui.table;
    var form = layui.form;
    var microcircle  = JSON.parse(localStorage.getItem("microcircle"));
    var comId = microcircle.microcircleId;
    setTable(microcircle);
    function setTable(microcircle){
        $('#name').text(microcircle.artcleTitle);
        $('#createDate').text(microcircle.releaseDate);
        $('#createName').text(microcircle.createName);
        $('#price').text('免费');
        $('#keyWords').text(microcircle.artcleKeywords);
        $('#categroyTitle').text(microcircle.categroyName);
        $('#likeNum').text(microcircle.acticleLike);
        $('#collectNum').text(microcircle.collectNum);
        $('#reviewsNum').text(microcircle.acticleReviews);
        $('#hits').text(microcircle.acticleHits);
        $('#share').text(microcircle.acticleShare);
        $('#description').text(microcircle.article);
        $('.picture').attr('src',microcircle.artcleUrl);
        var status = microcircle.details;
        if(status == 2){
            $('#status').text('草稿');
        }else if(status == 3){
            $('#status').text('已通过');
        }else if(status == 4){
            $('#status').text('未提交');
        }else if(status == 5){
            $('#status').text('已提交');
        }else if(status == 6){
            $('#status').text('退回');
        }
    }
    //用户赋值
    var user = JSON.parse(localStorage.getItem('user'));
    $('input[name=userName]').val(user.data.trueName);
    $('input[name=account]').val(user.data.account);
    var trueName = user.data.trueName;
    //获取全部评论
    getAllComment();
    function getAllComment(){
        $.ajax({
            url: '/comment/getALLComment',
            type: 'post',
            dataType: 'json',
            data: {'commodityId':comId,'type':4},
            success: function (data) {
                var list = data.data;
                var i = 0;
                for(i ; i < list.length ; i ++){
                    if(list[i].display == 2 && list[i].status != 3){
                        $('.all_body').append('<div id="all_item">'+'<img id="photo" src="'+list[i].imageUrl+'"/>&nbsp;' +
                            '<label id="comment_user">'+list[i].showName+'：'+'</label>&nbsp;' +
                            '<label id="comment_content">'+list[i].content+'</label>&nbsp;&nbsp;&nbsp;' +
                            '<label id="show_time">'+list[i].commentDate+'</label>&nbsp;&nbsp;&nbsp;' +
                            '<a onclick="javascript:hideComment('+list[i].id+','+list[i].display+');">隐藏</a>&nbsp;&nbsp;&nbsp;' +
                            '<a onclick="javascript:deleteTable('+list[i].id+');">删除</a>'+'</div>');
                    }else if(list[i].display == 3 && list[i].status != 3){
                        $('.all_body').append('<div id="all_item">'+'<img id="photo" src="'+list[i].imageUrl+'"/>&nbsp;' +
                            '<label id="comment_user">'+list[i].showName+'：'+'</label>&nbsp;' +
                            '<label id="comment_content">'+list[i].content+'</label>&nbsp;&nbsp;&nbsp;' +
                            '<label id="show_time">'+list[i].commentDate+'</label>&nbsp;&nbsp;&nbsp;' +
                            '<a onclick="javascript:hideComment('+list[i].id+','+list[i].display+');">显示</a>&nbsp;&nbsp;&nbsp;' +
                            '<a onclick="javascript:deleteTable('+list[i].id+');">删除</a>'+'</div>');
                    }else if(list[i].status == 3){
                        $('.all_body').append('<div id="all_item">'+'<img id="photo" src="'+list[i].imageUrl+'"/>&nbsp;' +
                            '<label id="comment_user">'+list[i].showName+'：'+'</label>&nbsp;' +
                            '<label id="comment_content">'+list[i].content+'</label>&nbsp;&nbsp;&nbsp;' +
                            '<label id="show_time">'+list[i].commentDate+'</label>&nbsp;&nbsp;&nbsp;' +
                            '<label style="color: #037EFE">已删除</label>'+'</div>');
                    }
                }
            }
        })
    }
    //提交评论
    form.on('submit(search)',function (obj) {
        var userType;
        if(trueName != undefined){
            userType = 2;
        }else{
            userType = 1;
        };
        var json = {
            userId: user.data.userId,
            parentId: $('#parentId').text(),
            commodityId: comId,
            content: obj.field.content,
            type: 4,
            userType: userType
        };
        $.ajax({
            url: '/comment/submitComment',
            type: 'post',
            dataType: 'json',
            data: json,
            success: function (data) {
            }
        })
    })
    //隐藏显示评论
    hideComment = function hideComment(id,display) {
        var dis = 0;
        if(display == 2){
            dis = 3
        }else if(display == 3){
            dis = 2
        };
        $.ajax({
            url: '/comment/showComment',
            type: 'post',
            dataType: 'json',
            data: {'id':id,'display':dis},
            success: function (data) {
                location.reload();
            }
        })

    }
    //删除
    deleteTable = function deleteTable(id){
        $.ajax({
            url: '/comment/deleteComment',
            type: 'post',
            dataType: 'json',
            data: {'id':id},
            beforeSend: function(){
                return confirm("确认删除？");
            },
            success: function (data) {
                if(data.data){
                    location.reload();
                }
            }
        })
    }
});